import { jobGoto } from "./goto";
import { jobSpread } from "./spread";

export const jobAttack = {
    /**
     * 
     * @param {Creep} creep 
     * @param {Array<AnyCreep | AnyStructure>} targets 
     * @param {MoveToOpts} opts 
     * @returns {CreepActionReturnCode}
     */
    run(creep, targets, opts=undefined){
        if(opts===undefined){
            opts = {visualizePathStyle:{stroke:"#ff0000"}, plainCost:1,swampCost:2};
        }
        if(targets.length === 0){
            jobSpread.run(creep);
            return ERR_INVALID_TARGET;
        }
        let gotoRet = jobGoto.run(creep, targets, opts);

        let attackRet;
        for(let target of targets){
            attackRet = creep.attack(target);
            if(attackRet === OK){
                return OK;
            }
            if(attackRet === ERR_NOT_IN_RANGE && gotoRet === OK){
                return OK;
            }
        }
        return attackRet;
    }
}